package com.dou.cloud.bridge.redis.handler;


import com.dou.cloud.bridge.redis.enums.RedissonEnum;
import com.due.basic.tookit.function.DueVoidProducer;
import org.redisson.api.RCountDownLatch;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
 * @Author daizl
 * @Date 2024 06 19 18 06
 **/
@Component
public class CountDownLatch extends AbstractDistributedLockHandler {
    @Override
    public <T> T tryLock(Supplier<Object> supplier, String key, long waitTime, long leaseTime, TimeUnit timeUnit) {
        isRedissonClient();
        RCountDownLatch latch = redissonClient.getCountDownLatch(key);
        latch.trySetCount(1);
        // 等待其他线程完成任务
        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public Integer getType() {
        return RedissonEnum.COUNTDOWN_LATCH.getType();
    }
}
